package org.jeecg.modules.xxgx.hzsqgl.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.common.util.LoginUserUtil;
import org.jeecg.modules.innerApi.common.convert.RequestParamConvert;
import org.jeecg.modules.innerApi.common.requestDto.*;
import org.jeecg.modules.innerApi.common.responseDto.*;
import org.jeecg.modules.innerApi.zhiYunApi.service.IZhiyunSkfpService;
import org.jeecg.modules.innerApi.zhiYunSdApi.responseVo.SdCxhzxxbcc112Rtn;
import org.jeecg.modules.innerApi.zhiYunSdApi.responseVo.SdSqqdphzxxbcc13Rtn;
import org.jeecg.modules.innerApi.zhiYunSdApi.service.IZhiyunSdfpService;
import org.jeecg.modules.qyxx.entity.CsPtQyxx;
import org.jeecg.modules.system.service.ISysDictService;
import org.jeecg.modules.xxgx.common.enums.*;
import org.jeecg.modules.xxgx.common.util.FPUtil;
import org.jeecg.modules.xxgx.common.util.InvoiceCalcUtil;
import org.jeecg.modules.xxgx.common.vo.HsBhsConvertVo;
import org.jeecg.modules.xxgx.fpgl.entity.CsXxFpxx;
import org.jeecg.modules.xxgx.fpgl.mapper.CsXxFpxxMapper;
import org.jeecg.modules.xxgx.fpgl.service.IFpkjglService;
import org.jeecg.modules.xxgx.hzsqgl.entity.CsXxHzsqjs;
import org.jeecg.modules.xxgx.hzsqgl.entity.CsXxHzsqjsmx;
import org.jeecg.modules.xxgx.hzsqgl.mapper.CsXxHzsqjsMapper;
import org.jeecg.modules.xxgx.hzsqgl.mapper.CsXxHzsqjsmxMapper;
import org.jeecg.modules.xxgx.hzsqgl.service.ICsXxHzsqjsService;
import org.jeecg.modules.xxgx.hzsqgl.vo.ConverCsxxHzsqjsPageVo;
import org.jeecg.modules.xxgx.hzsqgl.vo.CsXxHzsqjsPage;
import org.jeecg.modules.xxgx.kpzdgl.entity.CsXxKpzd;
import org.jeecg.modules.xxgx.kpzdgl.service.ICsXxKpzdService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description: 红字申请
 * @Author: jeecg-boot
 * @Date:   2023-09-15
 * @Version: V1.0
 */
@Slf4j
@Service
public class CsXxHzsqjsServiceImpl extends ServiceImpl<CsXxHzsqjsMapper, CsXxHzsqjs> implements ICsXxHzsqjsService {

	@Autowired
	private CsXxHzsqjsMapper csXxHzsqjsMapper;
	@Autowired
	private CsXxHzsqjsmxMapper csXxHzsqjsmxMapper;

	@Autowired
	private IZhiyunSkfpService zhiyunSkfpService;

	@Autowired
	private ICsXxKpzdService kpzdService;

	@Autowired
	private CsXxFpxxMapper csXxFpxxMapper;
	@Autowired
	private IZhiyunSdfpService zhiyunSdfpService;
	@Autowired
	@Lazy
	private IFpkjglService fpkjglService;

	@Autowired
	private ISysDictService sysDictService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(CsXxHzsqjs csXxHzsqjs, List<CsXxHzsqjsmx> csXxHzsqjsmxList) {
		csXxHzsqjsMapper.insert(csXxHzsqjs);
		if(csXxHzsqjsmxList!=null && csXxHzsqjsmxList.size()>0) {
			for(CsXxHzsqjsmx entity:csXxHzsqjsmxList) {
				//外键设置
				entity.setFromid(csXxHzsqjs.getId());
				csXxHzsqjsmxMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(CsXxHzsqjs csXxHzsqjs,List<CsXxHzsqjsmx> csXxHzsqjsmxList) {
		csXxHzsqjsMapper.updateById(csXxHzsqjs);

		//1.先删除子表数据
		csXxHzsqjsmxMapper.deleteByMainId(csXxHzsqjs.getId());

		//2.子表数据重新插入
		if(csXxHzsqjsmxList!=null && csXxHzsqjsmxList.size()>0) {
			for(CsXxHzsqjsmx entity:csXxHzsqjsmxList) {
				//外键设置
				entity.setFromid(csXxHzsqjs.getId());
				csXxHzsqjsmxMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		csXxHzsqjsmxMapper.deleteByMainId(id);
		csXxHzsqjsMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			csXxHzsqjsmxMapper.deleteByMainId(id.toString());
			csXxHzsqjsMapper.deleteById(id);
		}
	}

	/**
	 * 红字信息表添加、修改
	 *
	 * @param page
	 */
	public Result saveOrUpdate(CsXxHzsqjsPage page){
		Integer code=null;
		String msg="";
		ConverCsxxHzsqjsPageVo vo=convert(page);
		LoginUser user=LoginUserUtil.getLoginUser();
		CsPtQyxx qyxx=LoginUserUtil.getQyxxByLoginUser();

		if(StringUtils.isNotEmpty(vo.getError())){
			code=500;
			msg = vo.getError();
		}else{
			//转换红字申请数据
			//RequestParamConvert.hzxxbsqParamConvert(page);
			code=200;
			CsXxHzsqjs csXxHzsqjs = new CsXxHzsqjs();
			CsXxHzsqjs existsHzsqjs=this.isRepeat(page.getSqlsh());
			//当id为空时新增红字信息表
			if(existsHzsqjs != null){//根据红字流水号查询是否已保存到数据库，重复时，更新红字信息数据
				msg="红字信息更新成功";
				page.setUpdateBy(user.getUsername());
				page.setUpdateTime(new Date());
				//更新红字信息表
				BeanUtils.copyProperties(page, csXxHzsqjs);
				csXxHzsqjs.setId(existsHzsqjs.getId());
				this.updateMain(csXxHzsqjs,page.getCsXxHzsqjsmxList());
			}else{//不重复时，新增红字信息表
				String id=IdWorker.getIdStr();
				page.setId(id);
				page.setSysOrgCode(user.getOrgCode());
				page.setCompanyId(qyxx.getId());
				page.setCreateBy(user.getUsername());
				page.setTenantId(user.getLoginTenantId());
				page.setCreateTime(new Date());
				page.setClzt(HzclztEnum.zt0.getValue());
				page.setSczt(HzscztEnum.wsc.getValue());
				page.setKjzt(KpztEnum.wkp.getValue());
				page.setSqsj(new Date());
				BeanUtils.copyProperties(page, csXxHzsqjs);
				//保存红字信息
				this.saveMain(csXxHzsqjs, page.getCsXxHzsqjsmxList());
				msg="红字信息保存成功";
			}
		}
		Result rtn=new Result();
		rtn.setCode(code);
		if(code == 200){
			rtn.setSuccess(true);
		}else{
			rtn.setSuccess(false);
		}
		rtn.setMessage(msg);
		rtn.setResult(page);
		return rtn;
	}

	/***
	 * 转换页面传递的红字申请信息
	 * @param page
	 * @return
	 */
	private ConverCsxxHzsqjsPageVo convert(CsXxHzsqjsPage page){
		String error=null;
		String scbz=page.getSczt();
		if(StringUtils.isNotEmpty(scbz)&& HzscztEnum.ysc.getValue().equals(scbz)){
			error="该红字申请已上传，不能重复申请";
		}
		CsXxFpxx yfpxx = null;
		if(FplxdmEnum.sdzp.getValue().equals(page.getYfplxdm()) || FplxdmEnum.sdpp.getValue().equals(page.getYfplxdm())){
			if (!"".equals(page.getYfphm())) {
				yfpxx = csXxFpxxMapper.selectOne(new QueryWrapper<CsXxFpxx>()
						.eq("fphm", page.getYfphm()).last("limit 1")
				);
			}
		} else {
			if (!"".equals(page.getYfpdm()) && !"".equals(page.getYfphm())) {
				yfpxx = csXxFpxxMapper.selectOne(new QueryWrapper<CsXxFpxx>()
						.eq("fpdm", page.getYfpdm())
						.eq("fphm", page.getYfphm())
				);
			}
		}

		//设置清单标识
		List hcmxs=page.getCsXxHzsqjsmxList();
		String qdbz=page.getQdbz();
		//清单发票
		if(hcmxs!=null&&hcmxs.size()>8||(StringUtils.isNotEmpty(qdbz)&&qdbz.equals("1"))){
			page.setQdbz("1");
		}else{//非清单发票
			page.setQdbz("0");
		}
		//设置红冲类型
		if (yfpxx != null && yfpxx.getHjje() != null) {
			int compare = yfpxx.getHjje().compareTo(page.getHjje().negate());
			if (compare > 0) {
				//部分红冲
				page.setHclx("1");
			} else if (compare == 0) {
				page.setHclx("0");
			} else {
				error="红字申请金额不能大于原票金额";
			}
		} else {
			page.setHclx("1");
		}
		ConverCsxxHzsqjsPageVo vo=new ConverCsxxHzsqjsPageVo();
		vo.setError(error);
		vo.setPage(page);
		return vo;
	}

	/**
	 * 红字信息表申请
	 *
	 * @param page
	 */
	public Result hzxxbsq(CsXxHzsqjsPage page){
		Integer code=null;
		String msg="";
		Result rtn=new Result();
		String kpzddm=page.getKpzddm();
		Result<String> checkKpzddm=checkKpztdm(kpzddm,true);
		if(checkKpzddm.isSuccess()){
			kpzddm=checkKpzddm.getResult();
			page.setKpzddm(kpzddm);
		}else{
			msg=checkKpzddm.getMessage();
		}
		if(StringUtils.isNotEmpty(msg)){
			rtn.setCode(CommonConstant.SC_INTERNAL_SERVER_ERROR_500);
			rtn.setMessage(msg);
		}else{
			CommonParam commonParam=kpzdService.getCommonParamByKpzd(kpzddm);
			String zdlx=commonParam.getKpzd().getZdlx();
			if(zdlx.equals(KpzdlxEnum.skzd.getValue())) {//税控红字信息表申请
				rtn=hzxxbsqSk(page, commonParam);
			}else if(zdlx.equals(KpzdlxEnum.sdzd.getValue())){//数电红字信息表申请
				rtn=hzxxbsqSd(page,commonParam);
			}
		}
		if(rtn.getCode() == CommonConstant.SC_OK_200){
			rtn.setSuccess(true);
		}else{
			rtn.setSuccess(false);
		}
		return rtn;
	}
	/**
	 * 税控红字信息表申请
	 *
	 * @param page 红字申请信息
	 * @param param 终端、企业信息
	 */
	private Result hzxxbsqSk(CsXxHzsqjsPage page,CommonParam param){
		Integer code=null;
		String msg="";
		Object data=null;
		ConverCsxxHzsqjsPageVo vo=convert(page);
		LoginUser user=LoginUserUtil.getLoginUser();
		if(StringUtils.isNotEmpty(vo.getError())){
			code=CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
			msg = vo.getError();
		}else{
            //转换红字申请数据
			HzxxbsqParam hzxxbsqParam=RequestParamConvert.hzxxbsqParamConvert(page);;
			hzxxbsqParam.setKpzd(param.getKpzd());
			hzxxbsqParam.setFwqxx(param.getFwqxx());
			hzxxbsqParam.setQyxx(param.getQyxx());
			HzxxbsqRtn hzxxbsqRtn=zhiyunSkfpService.hzxxbsq(hzxxbsqParam);
			if(hzxxbsqRtn.getSuccess()){
				code=CommonConstant.SC_OK_200;
				msg="红字信息申请成功";
				data=hzxxbsqRtn.getData();
				String rtnMessage=hzxxbsqRtn.getMsg();
				if(rtnMessage.contains("申请成功")){
					page.setClzt(HzclztEnum.zt1.getValue());
				}
				if(rtnMessage.contains("上传成功")){
					page.setSczt(HzscztEnum.ysc.getValue());
				}
				String hzxxbbh = "";
				//正常的返回结果 红字信息表申请成功。红字上传信息：上传成功!信息表编号为： 1101022203000158
				//正则 提取16位的数字
				String regex = "\\d{16}";
				Matcher matcher = Pattern.compile(regex).matcher(msg);
				while (matcher.find()) {
					// 提取匹配到的数字
					hzxxbbh = matcher.group();
					page.setXxbbh(hzxxbbh);
				}
				CsXxHzsqjs csXxHzsqjs = new CsXxHzsqjs();
				BeanUtils.copyProperties(page, csXxHzsqjs);
				CsXxHzsqjs existsHzsqjs=this.isRepeat(page.getSqlsh());
				//当id为空时新增红字信息表
				if(existsHzsqjs != null){//根据红字流水号查询是否已保存到数据库，重复时，更新红字信息数据
					page.setUpdateBy(user.getUsername());
					page.setUpdateTime(new Date());
					csXxHzsqjs.setId(existsHzsqjs.getId());
					//更新红字信息表
					this.updateMain(csXxHzsqjs,page.getCsXxHzsqjsmxList());
				}else{//不重复时，新增红字信息表
					String id=IdWorker.getIdStr();
					csXxHzsqjs.setId(id);
					csXxHzsqjs.setSysOrgCode(user.getOrgCode());
					csXxHzsqjs.setCompanyId(param.getQyxx().getId());
					csXxHzsqjs.setCreateBy(user.getUsername());
					csXxHzsqjs.setTenantId(param.getQyxx().getTenantId());
					csXxHzsqjs.setCreateTime(new Date());
					csXxHzsqjs.setSqsj(new Date());
					//保存红字信息
					this.saveMain(csXxHzsqjs, page.getCsXxHzsqjsmxList());
				}
			}else{
				code=CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
				msg="红字信息申请失败：" + hzxxbsqRtn.getMsg();
			}
		}
		Result rtn=new Result();
		rtn.setCode(code);
		if(code == CommonConstant.SC_OK_200){
			rtn.setSuccess(true);
		}else{
			rtn.setSuccess(false);
		}
		rtn.setMessage(msg);
		rtn.setResult(data);
		return rtn;
	}
	/**
	 * 数电红字信息表申请
	 *
	 * @param page 红字申请信息
	 * @param param 终端、企业信息
	 */
	private Result hzxxbsqSd(CsXxHzsqjsPage page,CommonParam param){
		Result rtn=new Result();
		ConverCsxxHzsqjsPageVo vo=convert(page);
		if(StringUtils.isNotEmpty(vo.getError())){
			rtn.setMessage(vo.getError());
			rtn.setSuccess(false);
			rtn.setCode(CommonConstant.SC_INTERNAL_SERVER_ERROR_500);
			return rtn;
		}
		String sdjk_use_test = sysDictService.queryDictTextByKey("xxparams","sdjk_use_test");

		SdSqqdphzxxbcc13Rtn zyRtn;
		if(!"1".equals(sdjk_use_test)) {
			zyRtn = zhiyunSdfpService.hzxxbsq(page, param);
		} else {
			//cwgx todo: 红字申请返回模拟数据 start
			String json = "{\"msg\":\"成功\",\"success\":true,\"xxbbh\":\"41018823101001701087\",\"uuid\":\"827e173f8a1a40aba9197879ff896dbb\",\"xxbztDm\":\"02\",\"xxbzt\":\"销方录入待购方确认\"}";
			zyRtn = JSON.parseObject(json, SdSqqdphzxxbcc13Rtn.class);
			//cwgx todo: 红字申请返回模拟数据 end
		}

		if(zyRtn.getSuccess()){
			rtn.setCode(CommonConstant.SC_OK_200);
			rtn.setMessage("申请成功，红字信息表编号：" + zyRtn.getXxbbh() + "。状态：" + zyRtn.getXxbzt());
			page.setSczt(HzscztEnum.ysc.getValue());
			page.setXxbbh(zyRtn.getXxbbh());
			page.setHzzt(zyRtn.getXxbztDm());
			page.setClzt(HzclztEnum.zt1.getValue());
			CsXxHzsqjs csXxHzsqjs = new CsXxHzsqjs();
			BeanUtils.copyProperties(page, csXxHzsqjs);
			CsXxHzsqjs existsHzsqjs=this.isRepeat(page.getSqlsh());
			//当id为空时新增红字信息表
			if(existsHzsqjs != null){//根据红字流水号查询是否已保存到数据库，重复时，更新红字信息数据
				page.setUpdateTime(new Date());
				csXxHzsqjs.setId(existsHzsqjs.getId());
				//更新红字信息表
				this.updateMain(csXxHzsqjs,page.getCsXxHzsqjsmxList());
			}else{//不重复时，新增红字信息表
				String id=IdWorker.getIdStr();
				csXxHzsqjs.setId(id);
				csXxHzsqjs.setCompanyId(param.getQyxx().getId());
				csXxHzsqjs.setTenantId(param.getQyxx().getTenantId());
				csXxHzsqjs.setCreateTime(new Date());
				csXxHzsqjs.setSqsj(new Date());
				//保存红字信息
				this.saveMain(csXxHzsqjs, page.getCsXxHzsqjsmxList());
			}
			if(HzhcztEnum.zt01.getValue().equals(page.getHzzt())){
				//红字状态为01无需确认时，自动调用全电负数开具
				Result<InvoiceRtn.InvoiceRtnData> result = this.qdhzxxkj(page);

				if(result.isSuccess()) {
					InvoiceRtn.InvoiceRtnData invoiceRtnData = result.getResult();
					String fphm = invoiceRtnData.getFphm();
					log.info("红字申请成功，负数发票已开具，发票号码：s%",fphm);
					rtn.setMessage("红字申请成功，负数发票已开具，发票号码：" + fphm);
				}
			}
		}else{
			rtn.setCode(CommonConstant.SC_INTERNAL_SERVER_ERROR_500);
			rtn.setMessage(zyRtn.getMsg());
		}
		if(rtn.getCode() == CommonConstant.SC_OK_200){
			rtn.setSuccess(true);
		}else{
			rtn.setSuccess(false);
		}
		return rtn;
	}
	/**
	 * 红字信息表撤销:税控、数电
	 *
	 * @param param
	 */
	public Result hzxxbcx(HzxxbcxParam param){
		Integer code=null;
		String msg="";
		Object data=null;
		String hzxxbbh=param.getHzxxbbh();
		if(StringUtils.isEmpty(hzxxbbh)){
			msg="红字信息表编号不能为空";
			return Result.error(msg);
		}
		QueryWrapper<CsXxHzsqjs> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("xxbbh",hzxxbbh);
		CsXxHzsqjs hzsqjs=csXxHzsqjsMapper.selectOne(queryWrapper);
		if(hzsqjs==null){
			msg="未查询到"+hzxxbbh+"对应的红字信息";
			return Result.error(msg);
		}
		String kpzddm=param.getKpzddm();
		Result<String> checkKpzddm=checkKpztdm(kpzddm,true);
		if(checkKpzddm.isSuccess()){
			kpzddm=checkKpzddm.getResult();
		}else{
			msg=checkKpzddm.getMessage();
		}
		LoginUser user=LoginUserUtil.getLoginUser();
		if(StringUtils.isNotEmpty(msg)){
			code = CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
		}else {
			CommonParam commonParam = kpzdService.getCommonParamByKpzd(kpzddm);
			param.setNsrsbh(commonParam.getKpzd().getNsrsbh());
			param.setKpzd(commonParam.getKpzd());
			param.setFwqxx(commonParam.getFwqxx());
			param.setQyxx(commonParam.getQyxx());
			String zdlx=param.getKpzd().getZdlx();
			if(zdlx.equals(KpzdlxEnum.skzd.getValue())) {//税控红字信息表撤销
				CommonRtn hzxxbcxRtn = zhiyunSkfpService.hzxxbcx(param);
				if (hzxxbcxRtn.getSuccess()) {
					code = CommonConstant.SC_OK_200;
					msg = "红字信息撤销成功";
					hzsqjs.setClzt(HzclztEnum.zt4.getValue());
					hzsqjs.setUpdateTime(new Date());
					hzsqjs.setUpdateBy(user.getUsername());
					this.updateById(hzsqjs);
				} else {
					code = CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
					msg = "红字信息撤销失败：" + hzxxbcxRtn.getMsg();
				}
			}else if(zdlx.equals(KpzdlxEnum.sdzd.getValue())){//数电红字确认单撤销
				CommonRtn hzxxbcxRtn=zhiyunSdfpService.hzxxbcx(param);
				if (hzxxbcxRtn.getSuccess()) {
					code = CommonConstant.SC_OK_200;
					msg = "红字信息撤销成功";
					hzsqjs.setClzt(HzclztEnum.zt4.getValue());
					String hzzt=hzsqjs.getHzzt();
					if(StringUtils.isNotEmpty(hzzt)&&hzzt.equals(QdHzztEnum.zt4)){//确认后撤销
						hzsqjs.setHzzt(QdHzztEnum.zt9.getValue());
					}else{
						hzsqjs.setHzzt(QdHzztEnum.zt8.getValue());
					}
					hzsqjs.setUpdateTime(new Date());
					hzsqjs.setUpdateBy(user.getUsername());
					this.updateById(hzsqjs);
				} else {
					code = CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
					msg = "红字信息撤销失败：" + hzxxbcxRtn.getMsg();
				}
			}
		}
		Result rtn=new Result();
		rtn.setCode(code);
		if(code == CommonConstant.SC_OK_200){
			rtn.setSuccess(true);
		}else{
			rtn.setSuccess(false);
		}
		rtn.setMessage(msg);
		rtn.setResult(data);
		return rtn;
	}
	/**
	 * 红字信息表同步:税控、数电
	 *
	 * @param param
	 */
	public Result hzxxbtb(HzxxbtbParam param){
		Result rtn=new Result();
		String msg="";
		String kpzddm=param.getKpzddm();
		Result<String> checkKpzddm=checkKpztdm(kpzddm,true);
		if(checkKpzddm.isSuccess()){
			kpzddm=checkKpzddm.getResult();
		}else{
			msg=checkKpzddm.getMessage();
		}
		if(StringUtils.isNotEmpty(msg)){
			rtn.setCode(CommonConstant.SC_INTERNAL_SERVER_ERROR_500);
			rtn.setMessage(msg);
		}else {
			CommonParam commonParam = kpzdService.getCommonParamByKpzd(kpzddm);
			param.setKpzd(commonParam.getKpzd());
			param.setFwqxx(commonParam.getFwqxx());
			param.setQyxx(commonParam.getQyxx());
			param.setNsrsbh(commonParam.getKpzd().getNsrsbh());
			String zdlx=param.getKpzd().getZdlx();
			if(zdlx.equals(KpzdlxEnum.skzd.getValue())) {//税控红字信息表同步
				rtn=hzxxbtbSk(param);
			}else if(zdlx.equals(KpzdlxEnum.sdzd.getValue())){//数电红字确认单同步
				rtn=hzxxbtbSd(param);
			}
		}
		return rtn;
	}

	/**
	 * 税控红字信息表同步
	 */
	private Result hzxxbtbSk(HzxxbtbParam param){
		Result skrtn=new Result();
		Integer code=null;
		String msg="";
		Object data=null;
		HzxxbtbRtn hzxxbcxRtn = zhiyunSkfpService.hzxxbtb(param);
		if (hzxxbcxRtn.getSuccess()) {
			final CsPtQyxx qyxx = param.getQyxx();
			code=CommonConstant.SC_OK_200;
			msg="红字信息同步成功";
			data=hzxxbcxRtn.getData();
			List<HzxxbtbRtn.HzxxbtbRtnData> rtnData = hzxxbcxRtn.getData();
			if (rtnData != null && rtnData.size() > 0) {
				rtnData.forEach(item -> {
					String sqlsh = item.getSqlsh();
					QueryWrapper<CsXxHzsqjs> queryWrapper = new QueryWrapper<>();
					queryWrapper.eq("sqlsh", sqlsh);
					CsXxHzsqjs hzsqjs = csXxHzsqjsMapper.selectOne(queryWrapper);
					if(hzsqjs==null){
						hzsqjs = new CsXxHzsqjs();
					}
					String zyclbz = item.getClbz();
					String clzt = null, sczt = null;
					if (StringUtils.isEmpty(zyclbz) || zyclbz.equals("0")) {
						clzt = HzclztEnum.zt0.getValue();
						sczt = HzscztEnum.wsc.getValue();
					} else if (zyclbz.equals("1")) {
						clzt = HzclztEnum.zt2.getValue();
						sczt = HzscztEnum.ysc.getValue();
					} else if (zyclbz.equals("2")) {
						clzt = HzclztEnum.zt6.getValue();
						sczt = HzscztEnum.ysc.getValue();
					} else if (zyclbz.equals("3")) {
						clzt = HzclztEnum.zt7.getValue();
						sczt = HzscztEnum.ysc.getValue();
					} else if (zyclbz.equals("4")) {
						clzt = HzclztEnum.zt8.getValue();
						sczt = HzscztEnum.ysc.getValue();
					} else if (zyclbz.equals("5")) {
						clzt = HzclztEnum.zt5.getValue();
						sczt = HzscztEnum.ysc.getValue();
					} else if (zyclbz.equals("6")) {
						clzt = HzclztEnum.zt9.getValue();
						sczt = HzscztEnum.ysc.getValue();
					} else if (zyclbz.equals("7")) {
						clzt = HzclztEnum.zt10.getValue();
						sczt = HzscztEnum.ysc.getValue();
					} else if (zyclbz.equals("8")) {
						clzt = HzclztEnum.zt11.getValue();
						sczt = HzscztEnum.ysc.getValue();
					} else if (zyclbz.equals("9")) {
						clzt = HzclztEnum.zt12.getValue();
						sczt = HzscztEnum.ysc.getValue();
					} else if (zyclbz.equals("10")) {
						clzt = HzclztEnum.zt13.getValue();
						sczt = HzscztEnum.ysc.getValue();
					} else if (zyclbz.equals("11")) {
						clzt = HzclztEnum.zt14.getValue();
						sczt = HzscztEnum.ysc.getValue();
					} else if (zyclbz.equals("12")) {
						clzt = HzclztEnum.zt4.getValue();
						sczt = HzscztEnum.ysc.getValue();
					} else if (zyclbz.equals("13")) {
						clzt = HzclztEnum.zt15.getValue();
						sczt = HzscztEnum.ysc.getValue();
					}
					hzsqjs.setClzt(clzt);
					hzsqjs.setSczt(sczt);
					hzsqjs.setXxbbh(item.getXxbbh());
					if (hzsqjs != null&&hzsqjs.getId()!=null) {//更新红字信息表信息
						hzsqjs.setUpdateBy(null);
						hzsqjs.setUpdateTime(new Date());
						this.updateById(hzsqjs);
					} else {//新增同步到的红字信息表
						hzsqjs.setId(IdWorker.getIdStr());
						hzsqjs.setUpdateBy(null);
						hzsqjs.setUpdateTime(null);
						hzsqjs.setBz(null);
						hzsqjs.setCompanyId(qyxx.getId());
						hzsqjs.setCreateBy(null);
						hzsqjs.setCreateTime(new Date());
						hzsqjs.setHclx(item.getSqfs());
						hzsqjs.setHcyy(null);
						hzsqjs.setHjje(new BigDecimal(item.getHjje()));
						hzsqjs.setHjse(new BigDecimal(item.getHjse()));
						hzsqjs.setHzzt(null);
						hzsqjs.setKhdzdh(null);
						hzsqjs.setKhmc(item.getGhdwmc());
						hzsqjs.setKhsh(item.getGhdwsbh());
//						hzsqjs.setKjzt();
//						hzsqjs.setKhyhzh();
						hzsqjs.setLxdh(item.getLxfs());
						hzsqjs.setSbyy(null);
						String sqyy = "", sqf = "";
						sqyy = item.getSqyy();
						if (sqyy.equals("0")) {
							sqf = HzsqfEnum.gfsq_ydk.getValue();
						} else if (sqyy.equals("1")) {
							sqf = HzsqfEnum.gfsq_wdk.getValue();
						} else if (sqyy.equals("2")) {
							sqf = HzsqfEnum.xfsq.getValue();
						}
						hzsqjs.setSqf(sqf);
						hzsqjs.setSqlsh(item.getSqlsh());
						hzsqjs.setSqlx(HzsqlxEnum.zt0.getValue());
//						hzsqjs.setSqly();
						hzsqjs.setSqr(item.getSqrmc());
						hzsqjs.setSqsj(DateUtils.str2Date(item.getSqsj(), DateUtils.date_sdf.get()));
						hzsqjs.setTenantId(qyxx.getTenantId());
						hzsqjs.setSysOrgCode(qyxx.getSysOrgCode());
//						hzsqjs.setXfdzdh();
						hzsqjs.setXfmc(item.getXhdwmc());
						hzsqjs.setXfsh(item.getXhdwsbh());
//						hzsqjs.setXfyhzh();
						hzsqjs.setYfpdm(item.getLzfpdm());
						hzsqjs.setYfphm(item.getLzfphm());
						hzsqjs.setYfplxdm(item.getFplxdm());
//						hzsqjs.setYkprq();
						hzsqjs.setZsfs(item.getZsfs());
						String hsbs = HsbzEnum.Y.getValue();
						final String fromid=hzsqjs.getId();
						List<HzxxbtbRtn.HzxxbtbRtnDatamx> zbmxs=item.getZbmx();
						List<CsXxHzsqjsmx> csXxHzsqjsmxs=new ArrayList<>();
						String qdbz=QdbzEnum.N.getValue();
						if(zbmxs!=null&&zbmxs.size()>0){
							hsbs=zbmxs.get(0).getHsbz();
							if(zbmxs.size()>8){
								qdbz=QdbzEnum.Y.getValue();
							}
							for(HzxxbtbRtn.HzxxbtbRtnDatamx mx:zbmxs){
								CsXxHzsqjsmx csXxHzsqjsmx=new CsXxHzsqjsmx();
								csXxHzsqjsmx.setFromid(fromid);
								csXxHzsqjsmx.setTax(StringUtils.isNotEmpty(mx.getSl())?new BigDecimal(mx.getSl()):null);
								csXxHzsqjsmx.setTaxje(StringUtils.isNotEmpty(mx.getSe())?new BigDecimal(mx.getSe()):null);
								if(hsbs.equals(HsbzEnum.Y.getValue())){
									csXxHzsqjsmx.setHsdj(StringUtils.isNotEmpty(mx.getDj())?new BigDecimal(mx.getDj()):null);
									csXxHzsqjsmx.setHsje(StringUtils.isNotEmpty(mx.getJe())?new BigDecimal(mx.getJe()):null);
								}else{
									csXxHzsqjsmx.setBhsdj(StringUtils.isNotEmpty(mx.getDj())?new BigDecimal(mx.getDj()):null);
									csXxHzsqjsmx.setBhsje(StringUtils.isNotEmpty(mx.getJe())?new BigDecimal(mx.getJe()):null);
								}
								HsBhsConvertVo hsBhsConvertVo=new HsBhsConvertVo();
								BeanUtils.copyProperties(csXxHzsqjsmx,hsBhsConvertVo);
								FPUtil.hsBhsConvert(hsBhsConvertVo);
								BeanUtils.copyProperties(hsBhsConvertVo,csXxHzsqjsmx);
								csXxHzsqjsmx.setCompanyId(hzsqjs.getCompanyId());
								csXxHzsqjsmx.setFphxz(mx.getFphxz());
								csXxHzsqjsmx.setGgxh(mx.getGgxh());
								csXxHzsqjsmx.setId(IdWorker.getIdStr());
								csXxHzsqjsmx.setJldw(mx.getDw());
								csXxHzsqjsmx.setLslbs(mx.getLslbs());
								csXxHzsqjsmx.setMxhh(mx.getFpmxxh());
								csXxHzsqjsmx.setSpbm(mx.getSpbm());
								csXxHzsqjsmx.setSpmc(mx.getSpmc());
								csXxHzsqjsmx.setSpsl(StringUtils.isNotEmpty(mx.getSpsl())?new BigDecimal(mx.getSpsl()):null);
								csXxHzsqjsmx.setSsmc(StringUtils.isNotEmpty(mx.getSpsm())?mx.getSpsm():InvoiceCalcUtil.getGoodsCategory(mx.getSpmc()));
								csXxHzsqjsmx.setTenantId(hzsqjs.getTenantId());
								csXxHzsqjsmx.setYhzcbs(mx.getYhzcbs());
								csXxHzsqjsmx.setZzstsgl(mx.getZzstsgl());
								csXxHzsqjsmxs.add(csXxHzsqjsmx);
								String spmc=mx.getSpmc();
								if(StringUtils.isNotEmpty(spmc)&&(spmc.contains("详见销货清单") || spmc.contains("详见对应正数发票及清单"))){
									qdbz=QdbzEnum.Y.getValue();
								}
							}
						}
						hzsqjs.setQdbz(qdbz);
						hzsqjs.setHsbz(hsbs);
						this.saveMain(hzsqjs,csXxHzsqjsmxs);
					}
				});
			}
		} else {
			code=CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
			msg="红字信息同步失败：" + hzxxbcxRtn.getMsg();
		}
		skrtn.setCode(code);
		if(code == CommonConstant.SC_OK_200){
			skrtn.setSuccess(true);
		}else{
			skrtn.setSuccess(false);
		}
		skrtn.setMessage(msg);
		skrtn.setResult(data);
		return skrtn;
	}
	/**
	 * 数电红字信息表同步
	 */
	private Result hzxxbtbSd(HzxxbtbParam param){
		Result sdrtn=new Result();
		Integer code=null;
		String msg="";
		Object data=null;
		//数电发票先调用数电红字信息表同步接口，同步完成后，再调用查询接口查询同步到的红字信息表
		CommonRtn commonRtn = zhiyunSdfpService.hzxxbtb(param);
		if(commonRtn.getSuccess()){
			SdCxhzxxbcc112Rtn sdCxhzxxbcc112Rtn=zhiyunSdfpService.queryHzxxb(param);
			if(sdCxhzxxbcc112Rtn.getSuccess()){
				code=CommonConstant.SC_OK_200;
				msg="红字信息同步成功";
				data=sdCxhzxxbcc112Rtn.getData();
				List<SdCxhzxxbcc112Rtn.SdCxhzxxbcc112RtnData> rtnData = sdCxhzxxbcc112Rtn.getData();
				if(rtnData!=null&&rtnData.size()>0){
					if (rtnData != null && rtnData.size() > 0) {
						rtnData.forEach(item -> {
							String sqlsh = item.getSqlsh();
							QueryWrapper<CsXxHzsqjs> queryWrapper = new QueryWrapper<>();
							queryWrapper.eq("sqlsh", sqlsh);
							CsXxHzsqjs hzsqjs = csXxHzsqjsMapper.selectOne(queryWrapper);
							if (hzsqjs == null) {
								hzsqjs = new CsXxHzsqjs();
							}
							String zyclbz = item.getClbz();
							hzsqjs.setSczt(HzscztEnum.ysc.getValue());
							hzsqjs.setHzzt(zyclbz);
							String cxzt=item.getCxzt();//撤销状态：红字信息表如果撤销后该节点为（已撤销）否则为空
							if(StringUtils.isNotEmpty(cxzt)&&cxzt.contains("已撤销")){
								hzsqjs.setClzt(HzclztEnum.zt4.getValue());
							}else{
								hzsqjs.setClzt(HzclztEnum.zt1.getValue());
							}
							String hczt=item.getHczt();//红冲票开具状态
							if(StringUtils.isNotEmpty(hczt)&&hczt.equals("1")){
								hzsqjs.setKjzt(KpztEnum.ykp.getValue());
							}
							hzsqjs.setXxbbh(item.getXxbbh());
							if (hzsqjs != null&&hzsqjs.getId()!=null) {//更新红字信息表信息
								hzsqjs.setUpdateBy(null);
								hzsqjs.setUpdateTime(new Date());
								this.updateById(hzsqjs);
							}else{
								hzsqjs.setId(item.getId());
								hzsqjs.setYfplxdm(item.getFplxdm());
								hzsqjs.setYfphm(item.getLzfphm());
								hzsqjs.setYfpdm(item.getLzfpdm());
								if(StringUtils.isNotEmpty(item.getSqsj())){
								 hzsqjs.setSqsj(DateUtils.str2Date(item.getSqsj(),DateUtils.datetimeFormat.get()));
								}
								hzsqjs.setXfsh(item.getXhdwsbh());
								hzsqjs.setXfmc(item.getXhdwmc());
								hzsqjs.setKhsh(item.getGhdwsbh());
								hzsqjs.setKhmc(item.getGhdwmc());
								hzsqjs.setHjje(StringUtils.isNotEmpty(item.getHjje())?new BigDecimal(item.getHjje()):null);
								hzsqjs.setHjse(StringUtils.isNotEmpty(item.getHjse())?new BigDecimal(item.getHjse()):null);
								hzsqjs.setSqlsh(item.getSqlsh());
								if(StringUtils.isNotEmpty(item.getSqsj())) {
									hzsqjs.setYkprq(DateUtils.str2Date(item.getSqsj(),DateUtils.date_sdf.get()));
								}
								hzsqjs.setHcyy(item.getChyydm());

								String hsbs = HsbzEnum.Y.getValue();
								final String fromid=hzsqjs.getId();
								List<SdCxhzxxbcc112Rtn.SdCxhzxxbcc112RtnDatamx> zbmxs=item.getZbmx();
								List<CsXxHzsqjsmx> csXxHzsqjsmxs=new ArrayList<>();
								if(zbmxs!=null&&zbmxs.size()>0){
									hsbs=zbmxs.get(0).getHsbz();
									for(SdCxhzxxbcc112Rtn.SdCxhzxxbcc112RtnDatamx mx:zbmxs){
										CsXxHzsqjsmx csXxHzsqjsmx=new CsXxHzsqjsmx();
										csXxHzsqjsmx.setFromid(fromid);
										csXxHzsqjsmx.setTax(StringUtils.isNotEmpty(mx.getSl())?new BigDecimal(mx.getSl()):null);
										csXxHzsqjsmx.setTaxje(StringUtils.isNotEmpty(mx.getSe())?new BigDecimal(mx.getSe()):null);
										if(hsbs.equals(HsbzEnum.Y.getValue())){
											csXxHzsqjsmx.setHsdj(StringUtils.isNotEmpty(mx.getSpdj())?new BigDecimal(mx.getSpdj()):null);
											csXxHzsqjsmx.setHsje(StringUtils.isNotEmpty(mx.getJe())?new BigDecimal(mx.getJe()):null);
										}else{
											csXxHzsqjsmx.setBhsdj(StringUtils.isNotEmpty(mx.getSpdj())?new BigDecimal(mx.getSpdj()):null);
											csXxHzsqjsmx.setBhsje(StringUtils.isNotEmpty(mx.getJe())?new BigDecimal(mx.getJe()):null);
										}
										HsBhsConvertVo hsBhsConvertVo=new HsBhsConvertVo();
										BeanUtils.copyProperties(csXxHzsqjsmx,hsBhsConvertVo);
										FPUtil.hsBhsConvert(hsBhsConvertVo);
										BeanUtils.copyProperties(hsBhsConvertVo,csXxHzsqjsmx);
										csXxHzsqjsmx.setCompanyId(hzsqjs.getCompanyId());
										csXxHzsqjsmx.setFphxz(mx.getFphxz());
										csXxHzsqjsmx.setGgxh(mx.getGgxh());
										csXxHzsqjsmx.setId(IdWorker.getIdStr());
										csXxHzsqjsmx.setJldw(mx.getDw());
										csXxHzsqjsmx.setLslbs(mx.getLslbs());
										csXxHzsqjsmx.setMxhh(Integer.valueOf(mx.getFpmxxh()));
										csXxHzsqjsmx.setSpbm(mx.getSpbm());
										csXxHzsqjsmx.setSpmc(mx.getSpmc());
										csXxHzsqjsmx.setSpsl(StringUtils.isNotEmpty(mx.getSpsl())?new BigDecimal(mx.getSpsl()):null);
										csXxHzsqjsmx.setSsmc(StringUtils.isNotEmpty(mx.getSpsm())?mx.getSpsm():InvoiceCalcUtil.getGoodsCategory(mx.getSpmc()));
										csXxHzsqjsmx.setTenantId(hzsqjs.getTenantId());
										csXxHzsqjsmx.setYhzcbs(mx.getYhzcbs());
										csXxHzsqjsmx.setZzstsgl(mx.getZzstsgl());
										csXxHzsqjsmxs.add(csXxHzsqjsmx);
									}
								}
								hzsqjs.setHsbz(hsbs);
								this.saveMain(hzsqjs,csXxHzsqjsmxs);
							}
						 }
						);
					}
				}
			}else{
				code=CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
				msg=sdCxhzxxbcc112Rtn.getMsg();
			}
		}else{
			code=CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
			msg=commonRtn.getMsg();
		}
		sdrtn.setCode(code);
		sdrtn.setMessage(msg);
		sdrtn.setResult(data);
		if(code == CommonConstant.SC_OK_200){
			sdrtn.setSuccess(true);
		}else{
			sdrtn.setSuccess(false);
		}
        return sdrtn;
	}

	/**
	 * 检查开票终端代码
	 * @param kpzddm 开票终端代码
	 * @param IsCurUserKpzt 当kpzddm值为空时，是否从当前用户授权的状态中获取开票终端
	 */
	public Result checkKpztdm(String kpzddm, Boolean IsCurUserKpzt){
		String msg=null;
		Boolean checkRtn=true;
		if(StringUtils.isEmpty(kpzddm)) {
			if(IsCurUserKpzt) {
				List<CsXxKpzd> kpzds = LoginUserUtil.getKpzdsByUserKpzdsq("");
				CsXxKpzd kpzd = null;
				if (kpzds == null || kpzds.size() == 0) {
					msg = "当前用户暂未分配" + LoginUserUtil.getQyxxByLoginUser().getNsrmc() + "开票终端，请先进行开票终端授权";
					checkRtn = false;
				} else {
					kpzd = kpzds.get(0);
					kpzddm = kpzd.getZddm();
				}
			}else{
				checkRtn = false;
				msg = "开票终端代码为空！";
			}
		}
		Result result=new Result();
		result.setSuccess(checkRtn);
		result.setResult(kpzddm);
		result.setMessage(msg);
		return result;
	}
	/**
	 * 红字信息表上传
	 *
	 * @param param
	 */
	public Result hzxxbsc(HzxxbcxParam param){
		Integer code=null;
		String msg="";
		Object data=null;
		LoginUser user=LoginUserUtil.getLoginUser();
		String kpzddm=param.getKpzddm();
		Result<String> checkKpzddm=checkKpztdm(kpzddm,true);
		if(checkKpzddm.isSuccess()){
			kpzddm=checkKpzddm.getResult();
		}else{
			msg=checkKpzddm.getMessage();
		}
		if(StringUtils.isNotEmpty(msg)){
			code=CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
		}else{
			CommonParam commonParam=kpzdService.getCommonParamByKpzd(kpzddm);
			param.setKpzd(commonParam.getKpzd());
			param.setFwqxx(commonParam.getFwqxx());
			param.setQyxx(commonParam.getQyxx());
			CommonRtn commonRtn=zhiyunSkfpService.hzxxbsc(param);
			if(commonRtn.getSuccess()){
				QueryWrapper<CsXxHzsqjs> queryWrapper = new QueryWrapper<>();
				queryWrapper.eq("sqlsh",param.getSqlsh());
				CsXxHzsqjs hzsqjs=csXxHzsqjsMapper.selectOne(queryWrapper);
				hzsqjs.setSczt(HzscztEnum.ysc.getValue());
				hzsqjs.setUpdateBy(user.getUsername());
				hzsqjs.setUpdateTime(new Date());
				code=CommonConstant.SC_OK_200;
				msg="红字信息上传成功";
			}else{
				code=CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
				msg="红字信息上传失败：" + commonRtn.getMsg();
			}
		}
		Result rtn=new Result();
		rtn.setCode(code);
		rtn.setMessage(msg);
		rtn.setResult(data);
		if(code == CommonConstant.SC_OK_200){
			rtn.setSuccess(true);
		}else{
			rtn.setSuccess(false);
		}
		return rtn;
	}

	/**
	 * 红字信息表下载
	 *
	 * @param param
	 */
	public FileStreamRtn hzxxbxz(HzxxbxzParam param){
		String msg="";
		CsPtQyxx qyxx=LoginUserUtil.getQyxxByLoginUser();
		String kpzddm=param.getKpzddm();
		Result<String> checkKpzddm=checkKpztdm(kpzddm,true);
		if(checkKpzddm.isSuccess()){
			kpzddm=checkKpzddm.getResult();
		}else{
			msg=checkKpzddm.getMessage();
		}
		if(StringUtils.isNotEmpty(msg)){
			throw new JeecgBootException(msg);
		}
		CommonParam commonParam=kpzdService.getCommonParamByKpzd(kpzddm);
		if(KpzdlxEnum.sdzd.getValue().equals(commonParam.getKpzd().getZdlx())){
			throw new JeecgBootException("数电终端无红字下载！");
		}
		param.setKpzd(commonParam.getKpzd());
		param.setFwqxx(commonParam.getFwqxx());
		param.setQyxx(commonParam.getQyxx());
		return zhiyunSkfpService.hzxxbxz(param);
	}
	/**
	 * 根据红字申请流水号判断红字申请是否重复
	 *
	 * @param sqlsh
	 */
	public CsXxHzsqjs isRepeat(String sqlsh){
		if(StringUtils.isEmpty(sqlsh)){
			throw new JeecgBootException("申请流水号不能为空");
		}
		CsXxHzsqjs csXxHzsqjs = null;
		QueryWrapper<CsXxHzsqjs> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("sqlsh",sqlsh);
		List<CsXxHzsqjs> list=csXxHzsqjsMapper.selectList(queryWrapper);
		if(list!=null&&list.size()>0){
			csXxHzsqjs = list.get(0);
		}
        return csXxHzsqjs;
	}

	/**
	 * 全电红字信息表确认
	 *
	 * @param param
	 */
	public Result hzxxbqr(QdpHzxxbqrParam param){
		Integer code=null;
		String msg="";
		Object data=null;
		LoginUser user=LoginUserUtil.getLoginUser();
		CsPtQyxx qyxx=LoginUserUtil.getQyxxByLoginUser();
		String kpzddm=param.getKpzddm();
		Result<String> checkKpzddm=checkKpztdm(kpzddm,true);
		if(checkKpzddm.isSuccess()){
			kpzddm=checkKpzddm.getResult();
		}else{
			msg=checkKpzddm.getMessage();
		}
		if(StringUtils.isNotEmpty(msg)){
			code=CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
		}else{
			CommonParam commonParam=kpzdService.getCommonParamByKpzd(kpzddm);
			param.setKpzd(commonParam.getKpzd());
			param.setFwqxx(commonParam.getFwqxx());
			param.setQyxx(commonParam.getQyxx());
			CommonRtn commonRtn=zhiyunSdfpService.hzxxbNotarize(param);
			if(commonRtn.getSuccess()){
				QueryWrapper<CsXxHzsqjs> queryWrapper = new QueryWrapper<>();
				queryWrapper.eq("sqlsh",param.getXxbbh());
				CsXxHzsqjs hzsqjs=csXxHzsqjsMapper.selectOne(queryWrapper);
				String qrlx=param.getQrlx();
				String sqf=hzsqjs.getSqf();
				if(qrlx.equals("Y")){
					hzsqjs.setHzzt(QdHzztEnum.zt4.getValue());
				}else if(qrlx.equals("N")){
					if(sqf.startsWith("0")){//购方申请
						hzsqjs.setHzzt(QdHzztEnum.zt6.getValue());
					}else if(sqf.startsWith("1")){//销方申请
						hzsqjs.setHzzt(QdHzztEnum.zt5.getValue());
					}
				}
				hzsqjs.setUpdateBy(user.getUsername());
				hzsqjs.setUpdateTime(new Date());
				code=CommonConstant.SC_OK_200;
				msg="确认成功";
			}else{
				code=CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
				msg="确认失败：" + commonRtn.getMsg();
			}
		}
		Result rtn=new Result();
		rtn.setCode(code);
		rtn.setMessage(msg);
		rtn.setResult(data);
		if(code == CommonConstant.SC_OK_200){
			rtn.setSuccess(true);
		}else{
			rtn.setSuccess(false);
		}
		return rtn;
	}

	/**
	 * 全电根据红字信息开具负数发票
	 * @param page
	 * @return
	 */
	@Override
	public Result<InvoiceRtn.InvoiceRtnData> qdhzxxkj(CsXxHzsqjsPage page) {
		if(page.getKjzt() != null && !HzkjztEnum.wkj.getValue().equals(page.getKjzt())){
			throw new JeecgBootException("该红字申请单已开具，请核对！");
		}
		if(!HzhcztEnum.zt01.getValue().equals(page.getHzzt()) && !HzhcztEnum.zt04.getValue().equals(page.getHzzt())){
			throw new JeecgBootException("该红字申请单红字状态为" + HzhcztEnum.getDescByValue(page.getHzzt()) + "，不能开具负数发票！");
		}

		CommonParam commonParam=kpzdService.getCommonParamByKpzd(page.getKpzddm());
		String kpr = commonParam.getKpzd().getKpr();
		String skr = commonParam.getKpzd().getSkr();
		String fhr = commonParam.getKpzd().getFhr();

		InvoiceParam invoiceParam = new InvoiceParam();
		invoiceParam.setFpsqlsh(page.getSqlsh());
		invoiceParam.setKpzddm(page.getKpzddm());
		invoiceParam.setZsfs(page.getZsfs());
		invoiceParam.setKplx(KplxEnum.FSFP.getValue());
		invoiceParam.setKhmc(page.getKhmc());
		invoiceParam.setKhsh(page.getKhsh());
		invoiceParam.setKhdzdh(page.getKhdzdh());
		invoiceParam.setKhyhzh(page.getKhyhzh());
		invoiceParam.setFplxdm(page.getYfplxdm());
		invoiceParam.setTspz(page.getTspz());
		invoiceParam.setTdyslxdm(page.getTdyslxdm());
		invoiceParam.setXfsh(page.getXfsh());
		invoiceParam.setXfmc(page.getXfmc());
		invoiceParam.setXfdzdh(page.getXfdzdh());
		invoiceParam.setXfyhzh(page.getXfyhzh());
		invoiceParam.setKpr(kpr);
		invoiceParam.setSkr(skr);
		invoiceParam.setFhr(fhr);
		invoiceParam.setHsbz(page.getHsbz());
		invoiceParam.setHjje(page.getHjje());
		invoiceParam.setHjse(page.getHjse());
		invoiceParam.setQdbz(page.getQdbz());
		invoiceParam.setHzxxbbh(page.getXxbbh());
		invoiceParam.setYfphm(page.getYfphm());
		invoiceParam.setHcyy(page.getHcyy());
		invoiceParam.setBz("被红冲蓝字数电票号码："+invoiceParam.getYfphm()+" 红字发票信息确认单编号：" + invoiceParam.getHzxxbbh());

		List<InvoicemxParam> invoicemxParamList = new ArrayList<>();
		page.getCsXxHzsqjsmxList().forEach(hzsqmx -> {
			InvoicemxParam invoicemxParam = new InvoicemxParam();
			BeanUtils.copyProperties(hzsqmx, invoicemxParam);
			invoicemxParam.setId(IdWorker.getIdStr());
			invoicemxParamList.add(invoicemxParam);
		});
        invoiceParam.setFpmxs(invoicemxParamList);

		Result<InvoiceRtn.InvoiceRtnData> result = fpkjglService.invoice(invoiceParam);

		return result;
	}
}
